package com.lyw.GeoLocationClient.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import com.lyw.GeoLocationClient.App;
import com.lyw.GeoLocationClient.trackshow.TraceMainActivity;
import com.easemob.chatui.activity.ContactManagerActivity;
import com.easemob.chatui.activity.MainActivity;
import com.easemob.chatui.activity.MainSettingActivity;
import com.lyw.GeoLocationClient.R;
import com.lyw.GeoLocationClient.ui.adapter.LauncherGridAdapter;
import com.lyw.GeoLocationClient.ui.model.LauncherItem;

import java.util.ArrayList;

public class LauncherActivity extends Activity {
    private GridView mGridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher);

        mGridView = (GridView) findViewById(R.id.launcher_grid);

        final  ArrayList<LauncherItem>  list = new ArrayList<>();

        LauncherItem traffic = new LauncherItem(R.drawable.logo_uidemo, getString(R.string.traffic));
        traffic.setOnclickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                navToTraffic();
            }
        });
        LauncherItem lbs = new LauncherItem(R.drawable.logo_uidemo, getString(R.string.lbs));
        lbs.setOnclickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                navToLbsMgr();
            }
        });
        LauncherItem msg = new LauncherItem(R.drawable.logo_uidemo, getString(R.string.msg));
        msg.setOnclickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                navToMessage();
            }
        });
        LauncherItem user = new LauncherItem(R.drawable.logo_uidemo, getString(R.string.user_mgr));
        user.setOnclickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                navToUserMgr();
            }
        });
        LauncherItem settings = new LauncherItem(R.drawable.logo_uidemo, getString(R.string.setting));
        settings.setOnclickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                navToSettings();
            }
        });
        list.add(traffic);
        list.add(lbs);
        list.add(msg);
        list.add(user);
        list.add(settings);

        LauncherGridAdapter adapter =
                new LauncherGridAdapter(this, R.layout.launcher_item, list);
        mGridView.setAdapter(adapter);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                list.get(position).getListener().onItemClick(parent, view, position, id);
            }
        });

    }

    private void navToSettings() {
        startActivity(new Intent(this, MainSettingActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    }

    private void navToUserMgr() {
        startActivity(new Intent(this, ContactManagerActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    }

    private void navToMessage() {
        startActivity(new Intent(this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    }

    private void navToLbsMgr() {
        if (App.isAdmin()) {

        } else {
            startActivity(new Intent(this, TraceMainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
        }
    }

    private void navToTraffic() {
        startActivity(new Intent(this, TrafficLauncherActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    }

}

